package cn.kgc.fcm.dsbank.dto;

import cn.kgc.fcm.dsbank.enums.ResultEnum;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.io.Serializable;

@Data
public class ResultJson implements Serializable {

    private static final long serialVersionUID = 1l;

    private Integer code;       //相应编码
    private Boolean flag;       //是否成功
    private String msg;         //封装信息
    private Object data;        //数据

    public ResultJson(Integer code, String msg, Object data) {
        this.code = code;
        this.flag = code == ResultEnum.SUCCESS.getCode() ? true: false;
        this.msg = msg;
        this.data = data;
    }

    public static ResultJson buildSuccess(){
        return new ResultJson(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(),null);
    }

    public static ResultJson buildSuccess(Object data){
        return new ResultJson(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(),data);
    }

    public static ResultJson buildSuccess(String msg,Object data){
        return new ResultJson(ResultEnum.SUCCESS.getCode(), msg,data);
    }

    public static ResultJson buildError(String msg){
        return new ResultJson(ResultEnum.ERROR.getCode(), msg,null);
    }

    /**
     * 封装自定义的状态码和信息
     * @param code
     * @param message
     * @return
     */
    public static ResultJson build(int code, String message) {
       // logger.error("返回结果：code={}, message={}", code, message);
        return new ResultJson(code, message, null);
    }

    /**
     * 根据枚举对象封装相应结果
     * @param resultEnum
     * @return
     */
    public static ResultJson build(ResultEnum resultEnum) {
      //  logger.debug("返回结果：code={}, message={}", resultEnum.getCode(),
        //        resultEnum.getDesc());
        return new ResultJson(resultEnum.getCode(), resultEnum.getMsg(), null);
    }
}
